/**
 * 意见显示公共函数，只是做合并使用
 */

/**
 * 渲染意见列表
 */
function __rendList(comments, containerId, replyFlag, isRefresh, _containerId){
    
	//表情转换
	var emojiUtil = cmp.Emoji();
    for (var i = 0; i < comments.length; i++) {
    	comments[i].content = emojiUtil.StringToEmoji(comments[i].content);
    }
    var tplParams = {"comments" : comments, 
            "replyFlag": replyFlag,
            "containerId" : containerId,
            "canPraise" : summaryBO.summary.canPraise}
    var commentHTML = mplus_apps_collaboration.comment_template(tplParams); //cmp.tpl(commentTemplate, tplParams);
    
    
    if(isRefresh){
        _$("#" + containerId).innerHTML = commentHTML;
    }else{
        _$("#" + containerId).insertAdjacentHTML("beforeEnd",commentHTML); 
    }
    
    if(comments.length == 0 && _$("#"+ _containerId)){
        _$("#" + _containerId).remove();
    }
    
    //意见附件
    for(var i = 0; i < comments.length; i++){
        var comment = comments[i];
        var attrs = cmp.parseJSON(comment["attachments"]);
        if(attrs.length > 0 && comment.canView){
            CollUtils.initAttStyle("#" + containerId + "_commentAttr" + comment["id"],attrs);
        }else{
            _$("#" + containerId + "_commentAttr" + comment["id"]).remove();
        }
        //回复
        var subReplys = comment["subReplys"];
        if(subReplys && subReplys.length > 0){
            for(var j = 0; j < subReplys.length; j++){
                var subComment = subReplys[j];
                var subAttrsArray = subComment["attachments"] || "[]";
                var subAttrs = cmp.parseJSON(subAttrsArray);
                if(subAttrs.length > 0 && subComment.canView){
                    CollUtils.initAttStyle("#" + containerId + "_subCommentAttr" + subComment["id"], subAttrs);
                }else{
                    _$("#" + containerId + "_subCommentAttr" + subComment["id"]).remove();
                }
            }
        }
    }
}


/**
 * 意见点赞
 */
function _doLikeComment(){
    
    var commentId = this.getAttribute("commentId");
    var likeNum =parseInt(this.getAttribute("likeNum"), 10);
    var isAdd = true;
    if(this.classList.contains('cmp-active')){
        likeNum = likeNum - 1;
        isAdd = false;
    }else{
        //增加赞
        likeNum = likeNum + 1;
    }
    var likeBtns = document.querySelectorAll('.comment_like_btn_' + commentId);
    var likeNums = document.querySelectorAll('.comment_like_num_' + commentId);
    for(var i = 0, len = likeBtns.length;i<len;i++){
        likeNums[i].innerText = likeNum;
        likeBtns[i].setAttribute("likeNum", likeNum);
        if(isAdd){
            likeBtns[i].classList.add('cmp-active');
            likeNums[i].style.color = "#3AADFB";
        }else{
            likeBtns[i].classList.remove('cmp-active');
            likeNums[i].style.color = "#C7C7CC";
        }
    }
    $s.Coll.likeComment(commentId, {}, errorBuilder({
        success : function(result) {
            //
        }
    }));
}

//意见回复
function _showReplyBtns(container){
    
    //回复意见
    cmp(container).on("tap", ".replay_btn", function(){
        _replyComment(this.getAttribute("commentId"));
    });
    //点赞
    cmp(container).on("tap", ".comment_like_btn", _doLikeComment);
}

//回复意见
function _replyComment(commentId){
    
    var nextPageData = {
            "cache_subfix" : pageX.winParams.cache_subfix,
            "WebviewEvent" : WebEvents.M3_EVENT_SUMMARY + pageX.winParams.cache_subfix,
            "commentId" : commentId
    }
    cmp.event.trigger("beforepageredirect", document);
    cmp.href.next(_collPath + "/html/details/replyComment.html"+colBuildVersion, nextPageData);
}